#ifndef SWITCHCONTROL_H
#define SWITCHCONTROL_H

#include <QObject>
#include <QThread>
#include <QDebug>
#include <QTimer>
#include "modbus.h"
typedef struct SwitchInput
{
    bool success=false;
    bool onOff[4]={false,false,false,false};
} SwitchInput;

typedef struct SwitchControlValue
{
    bool sucess=false;
    bool onOff[4]={false,false,false,false};
} SwitchControlValue;

class Monitor:public QObject
{
    Q_OBJECT
public:
    Monitor();
    Modbus *modbus=nullptr;
    QThread thread;
    QVector <int>left,right;
    bool bRun=false;
    void StartThread();
public slots:
    void MonitorData();
signals:
    void StartMonitor();

};

class SwitchControl:public QObject
{
    Q_OBJECT
public:
    SwitchControl();
    ~SwitchControl();
    void StartSiganlInit();
    void SignalSlotsInit();
    void OpenDevice(QString);
    void SetSwitchOnOff(int RlayAdress,bool onof);          //供外部调用，打开或关闭继电器接口
    void StartTimer();                                      //供外部调用，启动定时器接口
    void StopTimer();                                       //供外部调用，停止定时器接口
    SwitchInput GetAllInputValue();                         //界面刷新按钮调用
    SwitchControlValue GetAllControlValue();                //界面另一个刷新按钮调用
    QThread thread;
    Modbus *modbus;
    QTimer *timer;
    QVector <int>left,right;
    QMutex locker;
    bool jumpQuerry=false;
    SwitchControlValue controlValue;
    bool isRunning=false;
    //Monitor monitor;
signals:
    void Signal_ToNewDevice();
    void Signal_ToOpenDevice(QString);
    void Signal_ToSetOnoff(int,bool);                         //控制继电器断开和闭合信号 SetSwitchOnOff(int RlayAdress,bool onof)调用
    void Signal_ToStartTimer();                               //启动定时器信号StartTimer()调用
    void Signal_ToStopTimer();                                //停止定时器信号StopTimer()调用
    void Signal_ToGetInputValue(bool*,unsigned char *);       //获得所有输入量的信号GetAllInputValue()调用
    void Signal_ToGetControlValue(bool*,unsigned char*);      //获得所有控制量的信号GetAllControlValue()调用
    void Signal_ToStartMonitor();
    void Signal_ToShowBumb(int);                              //光耦有输入，界面显示亮灯的信号。在mainwindows中关联ShowBumb(int)
public slots:
    void Slots_ToOpenDevice(QString);
    void Slots_ToNewDevice();
    void Slots_ToStartTimer();
    void Slots_ToStopTimer();
    void Slots_TimerTimeout();                                //定时器调用槽函数，定时读取光耦输入与开关控制量的值。
    void Slots_ToSetOnoff(int,bool);                          //设置开关开合的槽函数
    void Slots_ToGetInputValue(bool*,unsigned char *);        //获得所有输入量的槽函数 Signal_ToGetInputValue触发
    void Slots_ToGetControlValue(bool *,unsigned char *);     //获得所有控制继电器开关的槽函数 Signal_ToGetControlValue触发
};

#endif // SWITCHCONTROL_H
